package com.example.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.web.entity.Forum;
import com.example.web.entity.LeaveMessage;
import com.example.web.entity.util.ResultJson;
import com.example.web.entity.vo.FormChild;
import com.example.web.entity.vo.FormItem;
import com.example.web.entity.vo.ForumVo;
import com.example.web.mapper.LeaveMessageMapper;
import com.example.web.service.ForumService;
import com.example.web.service.LeaveMessageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.List;

/**
 * (Forum)表控制层
 *
 * @author makejava
 * @since 2024-05-05 12:53:35
 */
@RestController
@RequestMapping("forum")
public class ForumController {

    @Autowired
    private LeaveMessageService leaveMessageService;

    @Autowired
    private LeaveMessageMapper leaveMessageMapper;
    /**
     * 服务对象
     */
    @Autowired
    private ForumService forumService;


    @PostMapping("/getList")
    public ResultJson<Object> getList(@RequestBody ForumVo forum){
        return ResultJson.success(forumService.getList(forum));
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Forum selectOne(@PathVariable Serializable id) {
        return this.forumService.getById(id);
    }

    /**
     * 新增数据
     *
     * @param forum 实体对象
     * @return 新增结果
     */
    @PostMapping("save")
    public boolean insert(@RequestBody Forum forum) {
        return this.forumService.save(forum);
    }

    /**
     * 修改数据
     *
     * @param forum 实体对象
     * @return 修改结果
     */
    @PutMapping
    public boolean update(@RequestBody Forum forum) {
        return this.forumService.updateById(forum);
    }
    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public boolean delete(@RequestParam("idList") List<Integer> idList) {
        return this.forumService.removeByIds(idList);
    }

    @GetMapping("/selectDetailOne/{id}")
    public ResultJson<FormItem> selectDetailOne(@PathVariable Serializable id) {
        Forum forum = this.forumService.getById(id);

        LambdaQueryWrapper<LeaveMessage> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LeaveMessage::getGoodId,id);
        List<LeaveMessage> list = leaveMessageService.list(lambdaQueryWrapper);
        FormItem formItem=new FormItem();
        BeanUtils.copyProperties(forum, formItem);
        formItem.setCommentList(list);
        return ResultJson.success(formItem);
    }
}

